/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.testing;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.warehouse1.Warehouse1WS;
import org.warehouse1.Item;
import org.warehouse1.Warehouse1WSService;
import org.warehouse2.Warehouse2WS;
import org.warehouse2.Warehouse2WSService;
import org.warehouse3.Warehouse3WS;
import org.warehouse3.Warehouse3WSService;
import java.util.Random;
import org.manufacturers.Manufacturer1;
import org.manufacturers.Manufacturer2;
import org.manufacturers.Manufacturer3;
import org.manufacturers.Manufacturer1Service;
import org.manufacturers.Manufacturer2Service;
import org.manufacturers.Manufacturer3Service;
import org.manufacturers.PurchaseOrder;




/*
import org.warehouse1.*;
import org.warehouse2.*;
import org.warehouse3.*;
*/
/**
 *
 * @author Shaun McCready
 */
public class whTestServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {




        Manufacturer3Service manuSer = new Manufacturer3Service();
        Manufacturer3 manuWS3 = manuSer.getManufacturer3Port();

        List<PurchaseOrder> pOList = manuWS3.getOrders(true);


        Warehouse1WSService whservice1 = new Warehouse1WSService();
        Warehouse1WS whserve1 = whservice1.getWarehouse1WSPort();

        Warehouse2WSService whservice2 = new Warehouse2WSService();
        Warehouse2WS whserve2 = whservice2.getWarehouse2WSPort();

        Warehouse3WSService whservice3 = new Warehouse3WSService();
        Warehouse3WS whserve3 = whservice3.getWarehouse3WSPort();

        // getCatalog() test
        //java.util.List<Item> itemList = whserve1.getCatalog();

        
        //Generating a random number to pick which warehouse to go to
        //Random generator = new Random( 19580427 );
        //int randomNum = generator.nextInt(9999) % 3;
        int randomNum = 1;


        //This is the code for Warehouse 1
if(randomNum == 1)
{
    System.out.println("In Warehouse Number 1");



        String custref = request.getParameter("custrefnum");
        String manu1 = request.getParameter("manu1");
        String manu2 = request.getParameter("manu2");
        String manu3 = request.getParameter("manu3");
        String qty1 = request.getParameter("quantity1");
        String qty2 = request.getParameter("quantity2");
        String qty3 = request.getParameter("quantity3");

        org.warehouse1.Customer cust = new org.warehouse1.Customer();
        cust.setCity("Montreal");
        cust.setCountry("Canada");
        cust.setCustomerReferenceNumber(Integer.parseInt(custref));
        cust.setName("Shaun");
        cust.setState("Quebec");
        cust.setStreet1("123 Fake St");
        cust.setZip("A1B1C1");


        org.warehouse1.Item item1 = null;
        org.warehouse1.Item item2 = null;
        org.warehouse1.Item item3 = null;


        if(qty1 != null && !qty1.isEmpty())
        {
        item1 = new org.warehouse1.Item();
        item1.setManufacturerName(manu1);
        item1.setProductType("DVD Player");
        item1.setQuantity(Integer.parseInt(qty1));
        item1.setUnitPrice(30);
        }

        if(qty2 != null && !qty2.isEmpty())
        {
        item2 = new org.warehouse1.Item();
        item2.setManufacturerName(manu2);
        item2.setProductType("video camera");
        item2.setQuantity(Integer.parseInt(qty2));
        item2.setUnitPrice(60);
        }


        if(qty3 != null && !qty3.isEmpty())
        {
        item3 = new org.warehouse1.Item();
        item3.setManufacturerName(manu3);
        item3.setProductType("TV");
        item3.setQuantity(Integer.parseInt(qty3));
        item3.setUnitPrice(80);
        }


        ArrayList<org.warehouse1.Item> items = new ArrayList<org.warehouse1.Item>();
        if(item1 != null)
        items.add(item1);

        if(item2 != null)
        items.add(item2);

        if(item3 != null)
        items.add(item3);


        org.warehouse1.ItemShippingStatusList iss = whserve1.shipGoods(items, cust);


        request.setAttribute("result", iss);
        request.setAttribute("wh", "1");
}

else if(randomNum == 2)
{
    System.out.println("In Warehouse Number 2");



        String custref = request.getParameter("custrefnum");
        String manu1 = request.getParameter("manu1");
        String manu2 = request.getParameter("manu2");
        String manu3 = request.getParameter("manu3");
        String qty1 = request.getParameter("quantity1");
        String qty2 = request.getParameter("quantity2");
        String qty3 = request.getParameter("quantity3");

        org.warehouse2.Customer cust = new org.warehouse2.Customer();
        cust.setCity("Montreal");
        cust.setCountry("Canada");
        cust.setCustomerReferenceNumber(Integer.parseInt(custref));
        cust.setName("Shaun");
        cust.setState("Quebec");
        cust.setStreet1("123 Fake St");
        cust.setZip("A1B1C1");


        org.warehouse2.Item item1 = null;
        org.warehouse2.Item item2 = null;
        org.warehouse2.Item item3 = null;


        if(qty1 != null && !qty1.isEmpty())
        {
        item1 = new org.warehouse2.Item();
        item1.setManufacturerName(manu1);
        item1.setProductType("DVD Player");
        item1.setQuantity(Integer.parseInt(qty1));
        item1.setUnitPrice(30);
        }

        if(qty2 != null && !qty2.isEmpty())
        {
        item2 = new org.warehouse2.Item();
        item2.setManufacturerName(manu2);
        item2.setProductType("video camera");
        item2.setQuantity(Integer.parseInt(qty2));
        item2.setUnitPrice(60);
        }


        if(qty3 != null && !qty3.isEmpty())
        {
        item3 = new org.warehouse2.Item();
        item3.setManufacturerName(manu3);
        item3.setProductType("TV");
        item3.setQuantity(Integer.parseInt(qty3));
        item3.setUnitPrice(80);
        }


        ArrayList<org.warehouse2.Item> items = new ArrayList<org.warehouse2.Item>();
        if(item1 != null)
        items.add(item1);

        if(item2 != null)
        items.add(item2);

        if(item3 != null)
        items.add(item3);


        org.warehouse2.ItemShippingStatusList iss = whserve2.shipGoods(items, cust);


        request.setAttribute("result", iss);
        request.setAttribute("wh", "2");
}

else if(randomNum == 3)
{
    System.out.println("In Warehouse Number 3");



        String custref = request.getParameter("custrefnum");
        String manu1 = request.getParameter("manu1");
        String manu2 = request.getParameter("manu2");
        String manu3 = request.getParameter("manu3");
        String qty1 = request.getParameter("quantity1");
        String qty2 = request.getParameter("quantity2");
        String qty3 = request.getParameter("quantity3");

        org.warehouse3.Customer cust = new org.warehouse3.Customer();
        cust.setCity("Montreal");
        cust.setCountry("Canada");
        cust.setCustomerReferenceNumber(Integer.parseInt(custref));
        cust.setName("Shaun");
        cust.setState("Quebec");
        cust.setStreet1("123 Fake St");
        cust.setZip("A1B1C1");


        org.warehouse3.Item item1 = null;
        org.warehouse3.Item item2 = null;
        org.warehouse3.Item item3 = null;


        if(qty1 != null && !qty1.isEmpty())
        {
        item1 = new org.warehouse3.Item();
        item1.setManufacturerName(manu1);
        item1.setProductType("DVD Player");
        item1.setQuantity(Integer.parseInt(qty1));
        item1.setUnitPrice(30);
        }

        if(qty2 != null && !qty2.isEmpty())
        {
        item2 = new org.warehouse3.Item();
        item2.setManufacturerName(manu2);
        item2.setProductType("video camera");
        item2.setQuantity(Integer.parseInt(qty2));
        item2.setUnitPrice(60);
        }


        if(qty3 != null && !qty3.isEmpty())
        {
        item3 = new org.warehouse3.Item();
        item3.setManufacturerName(manu3);
        item3.setProductType("TV");
        item3.setQuantity(Integer.parseInt(qty3));
        item3.setUnitPrice(80);
        }


        ArrayList<org.warehouse3.Item> items = new ArrayList<org.warehouse3.Item>();
        if(item1 != null)
        items.add(item1);

        if(item2 != null)
        items.add(item2);

        if(item3 != null)
        items.add(item3);


        org.warehouse3.ItemShippingStatusList iss = whserve3.shipGoods(items, cust);


        request.setAttribute("result", iss);
        request.setAttribute("wh", "3");
}
else
{
            System.out.println("Major problems in choosing the random number generation withint the whTestServlet class");
}


        request.getRequestDispatcher("testShipGoods.jsp").forward(request, response);


    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
